<?php
class Lottery_prize_model extends BZ_Model
{
    protected $table   = 'lottery_prize';
    protected $table1  = 'prize_every_day';
    protected $table2  = 'activity_lottery';

	/**
     * 查询某抽奖活动的奖品总数
     */
    public function prizeTotal($params)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('audit_state <>', 4);
        if ( !empty($params['lottery_id'])) {
            $this->db->where('lottery_id', $params['lottery_id']);
        }
        return $this->db->count_all_results();
    }

	/**
     * 查询某抽奖活动的奖品列表
	 * @param number $page_num
	 * @param number $num
     * @param unknown $lottery_id
     */
    public function prizeList($page_num, $num, $params)
    {
        $this->db->select('prize_id, lottery_id, prize_name, status, audit_state, grade, type, amount, created_at');
        $this->db->from($this->table);
        $this->db->where('audit_state <>', 4);
        if ( !empty($params['lottery_id'])) {
            $this->db->where('lottery_id', $params['lottery_id']);
        }
		$this->db->order_by('prize_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

	/**
     * 查询某奖品信息
     * @param unknown $prize_id
     */
    public function findOnePrize($prizeId, $uid)
    {
        $this->db->select('prize_id, activity_lottery.lottery_id, prize_name, effective_days, activity_lottery.status, activity_lottery.audit_state, grade, image, type, amount, links');
        $this->db->from($this->table);
        $this->db->join($this->table2, 'activity_lottery.lottery_id = lottery_prize.lottery_id', 'INNER');
		$this->db->where('prize_id', $prizeId);
        $this->db->where('activity_lottery.uid', $uid);
		$this->db->limit(1);
        return $this->db->get();
    }

	/**
     * 更新奖品信息
     * @param unknown $prize_id
	 * @param array $data
     */
	public function updatePrize($postData, $imagesData)
	{
        $amount = 0;
        if ( !empty($postData['amount']) && $postData['type'] ==3) {
            $amount = $postData['amount'];
        }

        $data = array(
            'effective_days' => $postData['effective_days'],
            'prize_name'     => $postData['prize_name'],
            'status'         => $postData['status'],
            'grade'          => $postData['grade'],
            'image'          => $imagesData['file_name'],
            'type'           => $postData['type'],
            'amount'         => $amount,
            'links'          => isset($postData['links']) ? $postData['links'] : ''
        );
        if ( !empty($imagesData['file_name']) ) {
            $data['image'] = $imagesData['file_name'];
        }
		$this->db->where('prize_id', $postData['prize_id']);
		
        return $this->db->update($this->table,$data);
	}

	/**
     * 插入奖品
	 * @param array $data
     */
	public function insertPrize($postData, $imagesData)
	{
        $amount = 0;
        if ( !empty($postData['amount']) && $postData['type'] ==3) {
            $amount = $postData['amount'];
        }

        $data = array(
            'lottery_id'     => $postData['lottery_id'],
            'effective_days' => $postData['effective_days'],
            'audit_state'    => 1,
            'prize_name'     => $postData['prize_name'],
            'status'         => $postData['status'],
            'grade'          => $postData['grade'],
            'image'          => $imagesData['file_name'],
            'type'           => $postData['type'],
            'amount'         => $amount,
            'links'          => isset($postData['links']) ? $postData['links'] : '',
            'created_at'     => date('Y-m-d H:i:s'),
            'updated_at'     => '1970-01-01 08:00:01'
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
	}

	/**
     * 删除某奖品
     */
	public function deletePrize($prize_id)
	{
        $this->db->set('audit_state', 4);
        $this->db->where('prize_id', $prize_id);
		return $this->db->update($this->table);
	}

    /**
     * 更新一些信息
     */
    public function updateInfo($prizeId, $data)
    {
        $this->db->where('prize_id', $prizeId);
        return $this->db->update($this->table, $data);
    }

    /**
     * 查询某抽奖活动的所有奖品
     * @param unknown $lottery_id
     */
    public function findPrizes($params)
    {
        $this->db->select('lottery_id, lottery_prize.prize_id, type, prize_name,grade,image,quantity,surplus_quantity,probability,effective_days, links, amount, winners_user, time_slot');
        $this->db->from($this->table.' AS lottery_prize');
        $this->db->join($this->table1.' AS prize_every_day', 'lottery_prize.prize_id=prize_every_day.prize_id', 'INNER');
        $this->db->where('lottery_id', $params['lottery_id']);
        $this->db->where('prize_every_day.date', date('Y-m-d'));
        $this->db->where('lottery_prize.audit_state', 2);//审核通过
        $this->db->where('lottery_prize.status', 1);//上架
        $this->db->where('prize_every_day.status', 1);//上架
        $this->db->group_by('lottery_prize.prize_id');
        $this->db->order_by('grade', 'ASC');
        return $this->db->get();
    }
}